﻿namespace Trader.Analyzing
{
    using System;
    using BO.Interface;
    using Indicators.Interface;
    using Interface;

    public class MaxDayGradingKey : IGradingKey
    {
        private readonly StatisticsGenerator<MaxYieldByDays> statisticsGenerator;

        public MaxDayGradingKey()
        {
            var statisticFactory = new MaxYieldByDaysFactory();
            statisticsGenerator = new StatisticsGenerator<MaxYieldByDays>(statisticFactory);
        }

        #region IGradingKey Members

        ///<summary>
        ///Grade Indicator
        ///</summary>
        ///<param name="stock"></param>
        ///<param name="signal"></param>
        ///<param name="tradingDate"></param>
        ///<returns></returns>
        public decimal? GradeIndicator(IStock stock, ISignal signal, DateTime tradingDate)
        {
            var simulator = new StockSignalSimulator(stock, signal);
            IActionPoints actionPoints = simulator.Simulate(tradingDate);
            MaxYieldByDays statistic = statisticsGenerator.GenerateStatistic(stock, actionPoints);
            if (statistic.GetSuccessPercent() == null)
            {
                return null;
            }

            decimal successRate = statistic.GetSuccessPercent().Value;
            if (successRate < 65)
            {
                return null;
            }
            return Math.Round((decimal) (successRate*50M + (200M - statistic.GetVariance().Value)*25M +
                                         statistic.GetAverageYield().Value*250M +
                                         (100M - statistic.GetAverageDayWithMaxYield())*15M));
        }

        #endregion

        #region Nested type: MaxYieldByDaysFactory

        private class MaxYieldByDaysFactory : IStatisticFactory<MaxYieldByDays>
        {
            #region IStatisticFactory<MaxYieldByDays> Members

            public MaxYieldByDays CreateNewStatic()
            {
                return new MaxYieldByDays(30);
            }

            #endregion
        }

        #endregion
    }
}